package com.autumn.platform.dao.component.impl;

import java.io.InputStream;
import java.util.Map;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.apache.ibatis.builder.SqlSourceBuilder;
import org.apache.ibatis.builder.xml.XMLConfigBuilder;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.builder.xml.XMLStatementBuilder;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.parsing.XNode;
import org.apache.ibatis.scripting.xmltags.XMLScriptBuilder;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;

import com.autumn.platform.core.Defaults;
import com.autumn.platform.dao.component.IMybatisComponentFactory;

public class MybatisComponents {

    private static IMybatisComponentFactory getFactory() {
        return Defaults.getDefaultComponent(IMybatisComponentFactory.class);
    }

    /**
     * 创建SqlSessionFactoryBuilder对象
     * 
     * @return
     */
    public static SqlSessionFactoryBuilder newSqlSessionFactoryBuilder() {
        return getFactory().newSqlSessionFactoryBuilder();
    }

    /**
     * 创建mybatis全局配置解析器
     * 
     * @param inputStream
     * @param environment
     * @param props
     * @return
     */
    public static XMLConfigBuilder newXMLConfigBuilder(InputStream inputStream, String environment, Properties props) {
        return getFactory().newXMLConfigBuilder(inputStream, environment, props);
    }

    /**
     * 创建SqlMapper解析器
     * 
     * @param inputStream
     * @param configuration
     * @param resource
     * @param sqlFragments
     * @return
     */
    public static XMLMapperBuilder newXMLMapperBuilder(InputStream inputStream, Configuration configuration, String resource,
            Map<String, XNode> sqlFragments) {
        return getFactory().newXMLMapperBuilder(inputStream, configuration, resource, sqlFragments);
    }

    /**
     * 创建语句级元素解析器
     * 
     * @param configuration
     * @param builder
     * @param node
     * @param requiredDatabaseId
     * @return
     */
    public static XMLStatementBuilder newXMLStatementBuilder(Configuration configuration, MapperBuilderAssistant builder, XNode node, String requiredDatabaseId) {
        return getFactory().newXMLStatementBuilder(configuration, builder, node, requiredDatabaseId);
    }

    /**
     * 创建脚本级元素解析器
     * 
     * @param configuration
     * @param context
     * @param parameterType
     * @return
     */
    public static XMLScriptBuilder newXMLScriptBuilder(Configuration configuration, XNode node, Class<?> parameterType) {
        return getFactory().newXMLScriptBuilder(configuration, node, parameterType);
    }

    /**
     * 创建SqlSourceBuilder对象
     * 
     * @param configuration
     * @return
     */
    public static SqlSourceBuilder newSqlSourceBuilder(Configuration configuration) {
        return getFactory().newSqlSourceBuilder(configuration);
    }

    /**
     * 创建配置对象
     * 
     * @return
     */
    public static Configuration newConfiguration() {
        return getFactory().newConfiguration();
    }

    /**
     * 创建mybatis环境
     * 
     * @param id
     * @param transactionFactory
     * @param dataSource
     * @return
     */
    public static Environment newEnvironment(String id, TransactionFactory transactionFactory, DataSource dataSource) {
        return getFactory().newEnvironment(id, transactionFactory, dataSource);
    }

    /**
     * 创建mybatis事务工厂
     * 
     * @return
     */
    public static TransactionFactory newTransactionFactory() {
        return getFactory().newTransactionFactory();
    }

    /**
     * 创建参数处理器
     * 
     * @param mappedStatement
     * @param parameterObject
     * @param boundSql
     * @return
     */
    public static ParameterHandler newParameterHandler(MappedStatement mappedStatement, Object parameterObject,
            BoundSql boundSql) {
        return getFactory().newParameterHandler(mappedStatement, parameterObject, boundSql);
    }
}
